<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      label-position="left"
    >

      <div class="title-container">
        <h3 class="title">智慧农业监测系统</h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="studentid"
          v-model="loginForm.studentid"
          placeholder="账号"
          name="studentid"
          type="text"
          tabindex="1"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          @keyup.enter.native="handleLogin"
        />
        <!-- enter是按键的修饰符  native也是修饰符 -->
        <span
          class="show-pwd"
          @click="showPwd"
        >
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button
        class="loginBtn"
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:20px;"
        @click.native.prevent="handleLogin"
      >登录</el-button>
      <el-button
        class="resiterBtn"
        type="primary"
        style="width:100%;margin-bottom:30px; margin-left:0px;"
        @click.native.prevent="openDrawer('新用户注册')"
      >注册新用户</el-button>
      <!-- <el-button class="loginBtn" style="width:100%;margin-bottom:30px;" @click="openDrawer('新用户注册')">注册新用户</el-button> -->
      <div class="tips">
        <span style="margin-right:20px;">username: admin2023</span>
        <span>  password: 123456</span>
      </div>
    </el-form>
    <el-drawer
      :wrapper-closable="false"
      :title="drawerTitle"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
      :destroy-on-close="true"
    >
      <add-user v-if="drawerTitle==='新用户注册'" @closeDrawer="closeDrawer" />
    </el-drawer>
  </div>
</template>

<script>
import { mapActions } from 'vuex' // 引入vuex的辅助函数
import AddUser from '../users/components/add-user.vue'

export default {
  name: 'Login',
  components: {
    AddUser
  },
  data() {
    const validateStudentid = (rule, value, callback) => {
      if (value.length < 6 || value.length > 12) {
        callback(new Error('用户学号长度不小于6'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('用户密码不少于四位数，请重新输入'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        studentid: 'admin2023',
        password: '123456'
      },
      registForm: {
        studentname: '',
        studentid: '',
        sex: '',
        phone: '',
        password: ''
      },
      loginRules: {
        studentname: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' },
          { min: 10, max: 15, message: '长度在 10 到 15 个字符', trigger: 'blur' }
        ],
        studentid: [{ required: true, trigger: 'blur', validator: validateStudentid }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      drawerTitle: '注册新用户',
      drawer: false,
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      dialogFormVisible: false,
      // loading: false,
      photo: '',
      file: {},
      photoList: []
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions(['user/login']),
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async isOK => {
        if (isOK) {
          try {
            this.loading = true
            // 只有校验通过了 我们才去调用action
            // this.$store.commit('user/setUserId', this.loginForm.studentid)
            // this['user/setUserId'](this.loginForm.studentid)
            await this['user/login'](this.loginForm)
            // 应该登录成功之后
            // async标记的函数实际上一个promise对象
            // await下面的代码 都是成功执行的代码
            this.$router.push('/')
          } catch (error) {
            // console.log(error)
          } finally {
            //  不论执行try 还是catch  都去关闭转圈
            this.loading = false
          }
        }
      })
    },
    dialogClose() {
      this.registForm.studentname = ''
      this.registForm.studentid = ''
      this.registForm.password = ''
      this.dialogFormVisible = false
      this.$refs.registForm.resetFields()
    },
    changeFileList(val) {
      this.photoList = val
    },
    openDrawer(title) {
      this.drawerTitle = title
      this.drawer = true
    },
    closeDrawer() {
      const findForm = this.findForm
      for (const i in findForm) {
        findForm[i] = ''
      }
      this.findForm = findForm
      this.findUserInfo = {}
      this.drawer = false
      this.hasFin = false
    }
  }
}
</script>
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('~@/assets/login.jpg');
  background-position: center;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 32px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>

<style lang="scss">
$bg:#283443;
$light_gray:#68b0fe;
$cursor: #fff;

  .login-form{
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }

  .dialog-container{
    background:#2d3a4b;
    .el-dialog__title {
    line-height: 24px;
    font-size: 18px;
    color: #fff;
}
  }
  .el-form-item__label{
  color: #555;
}
.el-drawer__header > :first-child {
    outline: unset;
    font-size: 20px;
    font-weight: 600;
    color: #555;
}
.dialog-footer{
  text-align: center;
}
/*1.显示滚动条：当内容超出容器的时候，可以拖动：*/
.el-drawer__body {
   overflow: auto;
}
/*2.隐藏滚动条，太丑了*/
.el-drawer__container ::-webkit-scrollbar{
    display: none;
}
</style>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #68b0fe; // 将输入框颜色改成蓝色
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.login-container{
    background-image: url('~@/assets/login.jpg');
    background-position: center;
  }
/* reset element-ui css */
.login-form {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item__error {
    color: #fff;
  }
  .loginBtn {
    background: #407ffe;
    height: 54px;
    line-height: 32px;
    font-size: 20px;
  }
  .resiterBtn {
    background: rgba(255, 255, 255, 0.7);
    height: 54px;
    line-height: 32px;
    font-size: 20px;
    color: #000;
  }
}
</style>
